#include "stdafx.h"
#include "soundmanager.h"

#include <AL/alc.h>
#include <AL/al.h>

namespace ALC {

static
void checkOpenALError()
{
#ifdef _DEBUG
    ALenum ret = alGetError();
    if (ret == AL_NO_ERROR)
    {
        return;
    }

    myDebug() << "ALCDevice error: ";
    switch (ret)
    {
    case AL_INVALID_NAME:
        myDebug() << "A bad id passed to OpenAL function";
        break;
    case AL_INVALID_VALUE:
        myDebug() << "An invalid value passed to OpenAL function";
        break;
    case AL_INVALID_ENUM:
        myDebug() << "An invalid enum value passed to OpenAL function";
        break;
    case AL_INVALID_OPERATION:
        myDebug() << "Requested operation isn't valid";
        break;
    case AL_OUT_OF_MEMORY:
        myDebug() << "Requested operation is out of memory";
        break;
    }
    myDebug() << "\n";
#endif
}

SoundDevice::SoundDevice(const char *name)
{
    _device = reinterpret_cast<void*>(alcOpenDevice(name));
    checkOpenALError();
}

SoundDevice::~SoundDevice()
{
    ALCboolean status = alcCloseDevice(reinterpret_cast<ALCdevice*>(_device));
    if (status)
    {
        return;
    }

    myDebug() << "ALC::~ALCDevice() - device cannot be closed, "
              << "because it contains some contexts or buffers"
              << "\n";
}

SoundDevice::operator bool() const
{
    return (_device != NULL);
}

} // namespace ALC
